package com.cazor.common.utils;

import org.jasypt.encryption.pbe.PooledPBEStringEncryptor;
import org.jasypt.encryption.pbe.StandardPBEStringEncryptor;
import org.jasypt.encryption.pbe.config.EnvironmentPBEConfig;
import org.jasypt.encryption.pbe.config.SimpleStringPBEConfig;

public class JasyptUtil {

    /**
     * Jasyp2.x  加密
     * @param source 原字符串
     * @param passwd 密钥
     * @return
     */
    public static String encrypt(String source, String passwd){
        StandardPBEStringEncryptor standardPBEStringEncryptor = new StandardPBEStringEncryptor();
        EnvironmentPBEConfig config = new EnvironmentPBEConfig();
        config.setAlgorithm("PBEWithMD5AndDES");          // 加密的算法，这个算法是默认的
        config.setPassword(passwd);                        // 加密的密钥
        standardPBEStringEncryptor.setConfig(config);

        return standardPBEStringEncryptor.encrypt(source);
    }

    /**
     * Jasyp2.x  解密
     * @param encryptedString 加密字符串
     * @param passwd 密钥
     * @return
     */
    public static String decrypt(String encryptedString, String passwd){
        StandardPBEStringEncryptor standardPBEStringEncryptor = new StandardPBEStringEncryptor();
        EnvironmentPBEConfig config = new EnvironmentPBEConfig();
        config.setAlgorithm("PBEWithMD5AndDES");          // 加密的算法，这个算法是默认的
        config.setPassword(passwd);                        // 加密的密钥
        standardPBEStringEncryptor.setConfig(config);

        return standardPBEStringEncryptor.decrypt(encryptedString);
    }


    /**
     * Jasyp3.x  加密
     * @param source 原字符串
     * @param passwd 密钥
     * @return
     */
    public static String encrypt3(String source, String passwd){
        PooledPBEStringEncryptor encryptor = new PooledPBEStringEncryptor();
        // 2. 加解密配置
        SimpleStringPBEConfig config = new SimpleStringPBEConfig();
        config.setAlgorithm("PBEWithMD5AndDES");          // 加密的算法，这个算法是默认的
        config.setPassword(passwd);                        // 加密的密钥
        config.setKeyObtentionIterations( "1000");
        config.setPoolSize("1");
        config.setProviderName("SunJCE");
        config.setSaltGeneratorClassName("org.jasypt.salt.RandomSaltGenerator");
        config.setIvGeneratorClassName("org.jasypt.iv.RandomIvGenerator");
        config.setStringOutputType("base64");
        encryptor.setConfig(config);

        return encryptor.encrypt(source);
    }

    /**
     * Jasyp3.x  解密
     * @param encryptedString 加密字符串
     * @param passwd 密钥
     * @return
     */
    public static String decrypt3(String encryptedString, String passwd){
        PooledPBEStringEncryptor encryptor = new PooledPBEStringEncryptor();
        SimpleStringPBEConfig config = new SimpleStringPBEConfig();
        config.setAlgorithm("PBEWithMD5AndDES");          // 加密的算法，这个算法是默认的
        config.setPassword(passwd);                        // 加密的密钥
        config.setKeyObtentionIterations( "1000");
        config.setPoolSize("1");
        config.setProviderName("SunJCE");
        config.setSaltGeneratorClassName("org.jasypt.salt.RandomSaltGenerator");
        config.setIvGeneratorClassName("org.jasypt.iv.RandomIvGenerator");
        config.setStringOutputType("base64");
        encryptor.setConfig(config);

        return encryptor.decrypt(encryptedString);
    }

    /*public static void main(String[] args) {
        String source = "^_^ 你好 world!";

        String passwd = "P1cc@2019_1";
        String encryptedString = JasyptUtil.encrypt(source, passwd);

        String s = Base64.encodeBase64String(encryptedString.getBytes());
        System.out.println("encryptedString:"+s);

        String decryptedString  = JasyptUtil.decrypt(encryptedString, passwd);
        System.out.println("decryptedString:"+decryptedString);

        System.out.println(decryptedString.equals(source));

    }*/

}
